import React from 'react'
import Home from '@/components/home';

// 引入当前目录下面的所有的js文件
const ModulesFile = import.meta.glob('./*.tsx', { eager: true })

// 路由集合
const List: any[] = []

//提取每个对象下的值 同步获取
Object.values(ModulesFile).map((item: any) => {
    if (item.default) {
        List.push(...item.default)
    }
})



const RouterList = () => {

    //将用户的登录信息记录到状态数据中
    let cookie = React.Cookie.load('Business') ? React.Cookie.load('Business') : {};
    const [Business, SetBusiness] = React.useState(cookie);

    // 全局定义
    React.Business = Business;
    React.SetBusiness = SetBusiness;

    //生成一个路由的全局跳转方法
    const navigate = React.Router.useNavigate()
    React.navigate = navigate;

    //构建一个路由守卫的子组件
    const AuthRouter = (props: any) => {
        //判断当前路由是否有auth的属性
        if (props.auth) {
            //需要判断登录 没有属性，说明对象是空，没有登录
            if (Object.getOwnPropertyNames(Business).length <= 0) {
                return (
                    // 重定向跳转
                    <React.Router.Navigate to="/business/login" replace={true} />
                )
            }

            let id = Business.hasOwnProperty('id') ? Business.id : 0;
            let mobile = Business.hasOwnProperty('mobile') ? Business.mobile : '';

            let result:any = React.HTTP.post('/business/check', {
                id: id,
                mobile: mobile
            })

            // //异步操作
            result.then((result: any) => {
                if (result.code == 0) {

                    React.Cookie.remove('Business')

                    return (
                        <React.Router.Navigate to="/business/login" />
                    )
                } else {
                    //刷新覆盖一下cookie
                    React.Cookie.save('Business', result.data)
                    return (
                        <>
                            {props.component}
                        </>
                    )
                }
            })
            return (
                <>
                    {props.component}
                </>
            )
        }
        else {
            //不需要判断,直接显示当前要访问的组件
            return (
                <>
                    {props.component}
                </>
            )
        }
    }


    return (
        <>
            <React.Router.Routes>
                <React.Router.Route path='/' element={<Home />} />
                <React.Router.Route path="/" element={<><React.Router.Outlet /></>}>
                    {
                        List.map((item: any, index: number) => {
                            return (
                                <React.Router.Route key={index} path={item.path} element={<item.component />}>
                                    {
                                        item.children && item.children.map((child: any, indexs: number) => {
                                            return (
                                                <React.Router.Route key={indexs} path={child.path} element={<AuthRouter auth={child.auth ? child.auth : false} component={<child.component />}></AuthRouter>}/>
                                            )
                                        })
                                    }
                                </React.Router.Route>
                            )
                        })
                    }
                </React.Router.Route>
            </React.Router.Routes>
        </>
    )
}

export default RouterList;